package com.solocredit.finance.ui.main.adapter

import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import com.solocredit.finance.R
import com.solocredit.finance.app.MyApp
import com.solocredit.finance.base.adapter.MullaRecyclerViewAdapter
import com.solocredit.finance.base.adapter.MullaViewHolderHelper
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/8/8 16:42
 * @Annotation：优惠券列表适配器
 */

class MullaCouponAdapter(recyclerView: RecyclerView?) : MullaRecyclerViewAdapter<JSONObject>(recyclerView,R.layout.item_mulla_coupon_center) {

    //用户当前积分
    override fun setItemChildListener(helper: MullaViewHolderHelper, viewType: Int) {
        helper.setItemChildClickListener(R.id.itemLayout) //点击Item
        helper.setItemChildClickListener(R.id.btnExchange)//兑换
    }

    override fun fillData(helper: MullaViewHolderHelper, position: Int, model: JSONObject) {
        helper.setText(R.id.textAmount, model.optString("amount"))
        helper.setText(R.id.textUnit,model.optString("currency"))
        helper.setText(R.id.textMeetAmount,String.format(MyApp.app.getString(R.string.coupon_available),model.optString("fullAmount")))

        val score = model.optInt("score")
        helper.setText(R.id.textPoints,String.format(MyApp.app.getString(R.string.coupon_need_points),score.toString()))
        helper.setText(R.id.textUnlock,String.format(MyApp.app.getString(R.string.coupon_unlock),model.optString("vip")))

        val itemLayout = helper.getView<LinearLayout>(R.id.itemLayout)
        val btnExchange = helper.getTextView(R.id.btnExchange)
        //是否可兑换
        if (model.optBoolean("isExchange",false)){
            helper.setVisibility(R.id.unlockLayout, View.GONE)
            val userPoints = model.optInt("userPoints",0)
            //用户积分数 < 所需积分 - 不可兑换
            if (userPoints < score){
                itemLayout.setBackgroundResource(R.drawable.mulla_coupon_mall_item_bg_normal)
                btnExchange.text = MyApp.app.getString(R.string.coupon_not_enough)
                btnExchange.setBackgroundResource(R.drawable.mulla_radius_fill_ffb9b9b9_12)
            } else {
                itemLayout.setBackgroundResource(R.drawable.mulla_coupon_mall_item_bg_selected)
                btnExchange.text = MyApp.app.getString(R.string.coupon_exchange_now)
                btnExchange.setBackgroundResource(R.drawable.mulla_btn_pressed_12)
            }
        } else {
            helper.setVisibility(R.id.unlockLayout, View.VISIBLE)
            itemLayout.setBackgroundResource(R.drawable.mulla_coupon_mall_item_bg_selected)
            btnExchange.text = MyApp.app.getString(R.string.coupon_exchange_now)
            btnExchange.setBackgroundResource(R.drawable.mulla_btn_pressed_12)
        }
    }
}